var geocoder;
var map;
var marker;

function initialize() {
    var latlng = new google.maps.LatLng(-29.7109628, -53.71650484374999);
    var options = {
        zoom: 15,
        center: latlng,
        scrollwheel: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("mapa"), options);

    geocoder = new google.maps.Geocoder();

    marker = new google.maps.Marker({
        map: map,
        draggable: true,
    });

    //marker.setPosition(latlng);
}

function addMarker(lat, lon, point) {
    var latlng = new google.maps.LatLng(lat, lon);
    marker = new google.maps.Marker({
        map: map,
        draggable: false,
        title: point,
        windowText: point
    });

    marker.setPosition(latlng);
    texto = "<a class=\"btn btn-success btn-primary btn-xs\" title=\"Solicitar reserva\" href=\"/incorpore/Reservas/reserva/"+point+"\">Reserva</a>";

    var infowindow = new google.maps.InfoWindow(), marker;

    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infowindow.setContent(texto);
            infowindow.open(map, marker);
        }
    })(marker))
}

$(document).ready(function() {

    initialize();

    function carregarNoMapa(endereco) {
        geocoder.geocode({'address': endereco + ', Brasil', 'region': 'BR'}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[0]) {
                    var latitude = results[0].geometry.location.lat();
                    var longitude = results[0].geometry.location.lng();

                    $('#LaboratorioCentro').val(results[0].formatted_address);
                    $('#LaboratorioLatitude').val(latitude);
                    $('#LaboratorioLongitude').val(longitude);

                    var location = new google.maps.LatLng(latitude, longitude);
                    marker.setPosition(location);
                    map.setCenter(location);
                    map.setZoom(16);
                }
            }
        })
    }


    $("#btnEndereco").click(function() {
        if ($(this).val() != "")
            carregarNoMapa($("#LaboratorioCentro").val());
    })

    $("#LaboratorioCentro").blur(function() {
        if ($(this).val() != "")
            carregarNoMapa($(this).val());
    })

    google.maps.event.addListener(marker, 'drag', function() {
        geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[0]) {
                    $('#LaboratorioCentro').val(results[0].formatted_address);
                    $('#LaboratorioLatitude').val(marker.getPosition().lat());
                    $('#LaboratorioLongitude').val(marker.getPosition().lng());
                }
            }
        });
    });

    $("#LaboratorioCentro").autocomplete({
        source: function(request, response) {
            geocoder.geocode({'address': request.term + ', Brasil', 'region': 'BR'}, function(results, status) {
                response($.map(results, function(item) {
                    return {
                        label: item.formatted_address,
                        value: item.formatted_address,
                        latitude: item.geometry.location.lat(),
                        longitude: item.geometry.location.lng()
                    }
                }));
            })
        },
        select: function(event, ui) {
            $("#LaboratorioLatitude").val(ui.item.latitude);
            $("#LaboratorioLongitude").val(ui.item.longitude);
            var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
            marker.setPosition(location);
            map.setCenter(location);
            map.setZoom(16);
        }
    });
});